﻿using System;
using System.Collections.Generic;
using FrameIdentityReq.Network;

namespace FrameIdentityReq.Frames
{
    /// <summary>
    /// 身份验证请求帧
    /// </summary>
    public class IdentityReqFrame : BaseFrame
    {
        public static new event EventHandler<SocketState> OnRestored;
        /// <summary>
        /// 硬件号. 16 bytes. 紧邻 InfoLength 之后.
        /// </summary>
        public IStringField HardwareSerialNum;
        /// <summary>
        /// 用户名. 32 bytes. 紧邻 HardwareSerialNum 之后.
        /// </summary>
        public IStringField UserName;
        /// <summary>
        /// 密码. 32 bytes. 紧邻 UserName 之后.
        /// </summary>
        public IStringField PWD;

        public IdentityReqFrame() :
        base()
        {
            FrameType.Value = FrameTypeEnum.IDENTITY_REQ;
            InfoLength.Value = BitsField.CeilingByteCount(UserName.Count + PWD.Count);
        }

        public IdentityReqFrame(IList<byte> buffer) :
        base(buffer)
        { }

        protected override IField AddBodyFields(IField field)
        {
            HardwareSerialNum = new StringField(field, 16 << 3, "HardwareSerialNum");
            UserName = new StringField(HardwareSerialNum, 32 << 3, "UserName");
            PWD = new StringField(UserName, 32 << 3, "PWD");
            return PWD;
        }

        protected override void RaiseRestored(SocketState state)
        {
            if (OnRestored != null)
                OnRestored(this, state);
        }

    }
}
